package com.fifth.user.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fifth.common.pojo.dto.CommonResult;
import com.fifth.common.pojo.entity.BusinessInfo;
import com.fifth.common.pojo.entity.Collection;
import com.fifth.common.pojo.entity.Goods;
import com.fifth.user.mapper.CollectionMapper;
import com.fifth.user.service.CollectionService;
import com.fifth.user.util.WebUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 第五组
 * @since 2022-09-02
 */
@Service
public class CollectionServiceImpl implements CollectionService {
    @Autowired
    private CollectionMapper collectionMapper;
    @Autowired
    private StringRedisTemplate redisTemplate;

    @Override
    public CommonResult insertCollection(Collection collection) {
        HttpServletRequest request = WebUtils.getRequest();
        //获取token请求头
        String token = request.getHeader("token");
        //获取登陆者信息
        Map map= JSON.parseObject(redisTemplate.opsForValue().get(token),Map.class);
        Map user = (Map) map.get("user");
        collection.setUid((String) user.get("id"));
        if (collection.getBid()!=null){
            collection.setStatus(0);
            int insert = collectionMapper.insert(collection);
        }else {
            collection.setStatus(1);
            int insert = collectionMapper.insert(collection);
        }
        return new CommonResult(2000,"添加收藏成功",null);
    }

    @Override
    public CommonResult deleteCollection(String goodsid) {
        HttpServletRequest request = WebUtils.getRequest();
        String token = request.getHeader("token");
        //获取登陆者信息
        Map map= JSON.parseObject(redisTemplate.opsForValue().get(token),Map.class);
        Map user = (Map) map.get("user");
        String uid = (String) user.get("id");
        QueryWrapper<Collection> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("uid",uid);
        queryWrapper.eq("goodsid",goodsid);
        int i = collectionMapper.delete(queryWrapper);
        if (i!=0){
            return new CommonResult(2000,"删除成功",null);
        }
        return new CommonResult(5000,"删除失败",null);
    }

    @Override
    public CommonResult findBid() {
        HttpServletRequest request = WebUtils.getRequest();
        //获取token请求头
        String token = request.getHeader("token");
        //获取登陆者信息
        Map map= JSON.parseObject(redisTemplate.opsForValue().get(token),Map.class);
        Map user = (Map) map.get("user");
        String uid = (String) user.get("id");
        List<BusinessInfo> list = collectionMapper.selectBid(uid);
        return new CommonResult(2000,"查询成功",list);
    }

    @Override
    public CommonResult findGid() {
        HttpServletRequest request = WebUtils.getRequest();
        //获取token请求头
        String token = request.getHeader("token");
        //获取登陆者信息
        Map map= JSON.parseObject(redisTemplate.opsForValue().get(token),Map.class);
        Map user = (Map) map.get("user");
        String uid = (String) user.get("id");
        List<Goods> list = collectionMapper.selectGid(uid);
        return new CommonResult(2000,"查询成功",list);
    }

    @Override
    public CommonResult findByBid(String id) {
        QueryWrapper<Collection> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("bid",id);
        queryWrapper.eq("is_deleted",0);
        queryWrapper.eq("status",0);
        Collection collection = collectionMapper.selectOne(queryWrapper);
        if (collection!=null){
            return new CommonResult(2000,"该商家已收藏",collection);
        }
        return new CommonResult(4000,"该商家未收藏",collection);
    }

    @Override
    public CommonResult findByGoodsid(String id) {
        QueryWrapper<Collection> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("goodsid",id);
        queryWrapper.eq("is_deleted",0);
        queryWrapper.eq("status",1);
        Collection collection = collectionMapper.selectOne(queryWrapper);
        if (collection!=null){
            return new CommonResult(2000,"该商品已收藏",collection);
        }
        return new CommonResult(4000,"该商品未收藏",collection);
    }

    @Override
    public CommonResult deleteBid(String bid) {
        HttpServletRequest request = WebUtils.getRequest();
        //获取token请求头
        String token = request.getHeader("token");
        //获取登陆者信息
        Map map= JSON.parseObject(redisTemplate.opsForValue().get(token),Map.class);
        Map user = (Map) map.get("user");
        String uid = (String) user.get("id");
        QueryWrapper<Collection> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("bid",bid);
        queryWrapper.eq("uid",uid);
        int delete = collectionMapper.delete(queryWrapper);
        if (delete!=0){
            return new CommonResult(2000,"删除成功",null);
        }
        return new CommonResult(5000,"删除失败",null);
    }

    @Override
    public Collection isGoodsCollection(String goodsid, String uid) {
        QueryWrapper<Collection> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_deleted",0);
        queryWrapper.eq("goodsid",goodsid);
        queryWrapper.eq("uid",uid);
        Collection collection = collectionMapper.selectOne(queryWrapper);
        return collection;
    }

    @Override
    public CommonResult insertGid(String goodsid, String uid) {
        Collection collection = new Collection();
        collection.setUid(uid);
        collection.setGoodsid(goodsid);
        collection.setStatus(1);
        int insert = collectionMapper.insert(collection);
        return new CommonResult(2000,"添加成功",null);
    }

    @Override
    public CommonResult deleteGid(String goodsid, String uid) {
        QueryWrapper<Collection> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("goodsid",goodsid);
        queryWrapper.eq("uid",uid);
        int delete = collectionMapper.delete(queryWrapper);
        return new CommonResult(2000,"删除成功",null);
    }

    @Override
    public CommonResult insertBid(String bid, String uid) {
        Collection collection = new Collection();
        collection.setUid(uid);
        collection.setBid(bid);
        collection.setStatus(0);
        int insert = collectionMapper.insert(collection);
        return new CommonResult(2000,"添加成功",null);
    }

    @Override
    public CommonResult deleteByBid(String bid, String uid) {
        QueryWrapper<Collection> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("bid",bid);
        queryWrapper.eq("uid",uid);
        int delete = collectionMapper.delete(queryWrapper);
        return new CommonResult(2000,"删除成功",null);
    }

    @Override
    public Collection selectBidAUid(String bid, String uid) {
        QueryWrapper<Collection> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_deleted",0);
        queryWrapper.eq("bid",bid);
        queryWrapper.eq("uid",uid);
        Collection collection = collectionMapper.selectOne(queryWrapper);
        return collection;
    }
}
